package cn.mxb.logic.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.ghs.jms.MessageReceiver;
import com.ghs.jms.MessageSender;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import cn.mxb.floor.pagination.Result;
import cn.mxb.floor.web.BaseAction;
import cn.mxb.logic.entity.SysCount;
import cn.mxb.logic.entity.SysTown;
import cn.mxb.logic.entity.SysVillage;
import cn.mxb.logic.entity.SysZu;
import cn.mxb.logic.service.ISysCountService;
import cn.mxb.logic.service.ISysTownService;
import cn.mxb.logic.service.ISysVillageService;
import cn.mxb.logic.service.ISysZuService;

@SuppressWarnings({ "serial", "rawtypes" })
public class SysZuAction extends BaseAction implements Preparable,ModelDriven {
	protected static final String List_JSP = "/WEB-INF/page/web/floor/zuList.jsp";
	protected static final String ADD_JSP = "/WEB-INF/page/web/floor/zuAdd.jsp";
	protected static final String UPDATE_JSP = "/WEB-INF/page/web/floor/zuUpdate.jsp";
	
	private SysZu sysZu;
	@Autowired
	private ISysZuService sysZuService;
	@Autowired
	private ISysCountService sysCountService;
	@Autowired
	private ISysTownService sysTownService;
	@Autowired
	private ISysVillageService sysVillageService;
	
	private Result<SysZu> pageResult;
	
	private List<SysZu> sysZuAllList; 
	private List<SysCount> sysCountAllList; 
	private List<SysTown> sysTownAllList; 
	private List<SysVillage> sysVillageAllList; 
	
	private Map<Integer,String> countNameMap = new HashMap<Integer, String>();  //县名
	private Map<Integer,String> townNameMap = new HashMap<Integer, String>();  //镇名
	private Map<String,String> villageNameMap = new HashMap<String, String>();  //村名
	
	/**
	 * @return_type: String
	 * @file_name: 跳转添加界面
	 */
	public String openAdd(){
		sysCountAllList = sysCountService.findAllList();
		sysTownAllList = sysTownService.findAllList();
		sysVillageAllList = sysVillageService.findAllList();
		setForwardView(ADD_JSP);
		/* try {
				MessageSender.Sender(null);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		MessageReceiver.Receiver(null);
*/	   
		
		return SUCCESS;
	}
	/**
	 * 邮件
	 */
	public void fasong(){
		try {
			MessageSender.Sender(null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	MessageReceiver.Receiver(null);
	
	}
	
	/**
	 * 添加
	 * @return_type: String
	 */
	public String add(){
		System.out.println("add");
		sysZuService.add(sysZu);
		return openList();		
	}
	
	/**
	 * @file_name 打开列表
	 * @return 
	 */
	public String openList(){
		try{
			pageResult = sysZuService.find(sysZu, getPage(), getRow());
			for(SysZu sysZu : pageResult.getData()){
				if(null != sysZu.getCountId()  && !"".equals(sysZu.getCountId())){
					countNameMap.put(sysZu.getCountId(),sysCountService.findByProperty("countId", sysZu.getCountId()+"").getCountName());
				}
				if(null != sysZu.getTownId() && !"".equals(sysZu.getTownId())){
					townNameMap.put(sysZu.getTownId(),sysTownService.findByProperty("sysTownId", sysZu.getTownId()+"").getSysTownName());
				}
				if(null != sysZu.getVillageId() && !"".equals(sysZu.getVillageId())){
					villageNameMap.put(sysZu.getVillageId(),sysVillageService.findByProperty("villageId", sysZu.getVillageId()+"").getVillageName());
				}
			}
			setForwardView(List_JSP);
			return SUCCESS;
		}catch(Exception e){
			System.out.println(e);
		}
		return SUCCESS;
	}
	
	/**
	 * @file_name:删除 
	 */
	public String delete(){
		sysZuService.delete(sysZu.getId());
		return openList();
	}
	
	/**
	 * @return_type: 
	 * @file_name:  打开修改页面
	 */
	public String openUpdate(){
		sysZu =  sysZuService.findByProperty("id",sysZu.getId()+"");
		sysCountAllList = sysCountService.findAllList();
		sysTownAllList = sysTownService.findAllList();
		sysVillageAllList = sysVillageService.findAllList();
		setForwardView(UPDATE_JSP);
		return SUCCESS;
		
	}
	
	/**
	 * 修改
	 * @return_type: 
	 */
	public String update(){
		sysZuService.add(sysZu);
		sysZu = null;
		return openList();
	}
	
	
	@Override
	public Object getModel() {
		return sysZu;
	}
	@Override
	public void prepare() throws Exception {
		if(sysZu ==null){
			sysZu = new SysZu();
		}
	}


	public SysZu getSysZu() {
		return sysZu;
	}

	public void setSysZu(SysZu sysZu) {
		this.sysZu = sysZu;
	}

	public Result<SysZu> getPageResult() {
		return pageResult;
	}

	public void setPageResult(Result<SysZu> pageResult) {
		this.pageResult = pageResult;
	}
	
	public List<SysCount> getSysCountAllList() {
		return sysCountAllList;
	}

	public void setSysCountAllList(List<SysCount> sysCountAllList) {
		this.sysCountAllList = sysCountAllList;
	}

	public List<SysTown> getSysTownAllList() {
		return sysTownAllList;
	}

	public void setSysTownAllList(List<SysTown> sysTownAllList) {
		this.sysTownAllList = sysTownAllList;
	}

	public Map<Integer, String> getCountNameMap() {
		return countNameMap;
	}

	public void setCountNameMap(Map<Integer, String> countNameMap) {
		this.countNameMap = countNameMap;
	}

	public Map<Integer, String> getTownNameMap() {
		return townNameMap;
	}

	public void setTownNameMap(Map<Integer, String> townNameMap) {
		this.townNameMap = townNameMap;
	}

	

	public Map<String, String> getVillageNameMap() {
		return villageNameMap;
	}

	public void setVillageNameMap(Map<String, String> villageNameMap) {
		this.villageNameMap = villageNameMap;
	}

	public List<SysVillage> getSysVillageAllList() {
		return sysVillageAllList;
	}

	public void setSysVillageAllList(List<SysVillage> sysVillageAllList) {
		this.sysVillageAllList = sysVillageAllList;
	}

	public List<SysZu> getSysZuAllList() {
		return sysZuAllList;
	}

	public void setSysZuAllList(List<SysZu> sysZuAllList) {
		this.sysZuAllList = sysZuAllList;
	}


	

	
	
}
